
//check if two password are the same
var  password = document.getElementById('repw');
password.addEventListener('change', checkpassword, true);

function checkpassword(){
 var firstPassword = document.getElementById('pw').value;
 var secondPassword = document.getElementById('repw').value;
 
if (firstPassword != secondPassword){
alert("You must enter same password, please try again!");
document.getElementById('repw').value='';
document.getElementById('pw').value='';}}

//check if the email is valid
 var  email = document.getElementById('email');
 email.addEventListener('change', checkmail, true);
 
function checkmail() 
{ 
var emailAddress = document.getElementById('email').value;
var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
if (filter.test(emailAddress))
testresults=true
else{
alert("You must enter a valid email address!")
testresults=false
}
return (testresults)

} 
 
 //Check if the all field are filled 
 var submit = document.getElementById('submit');
  submit.addEventListener('click',checkEmpty,true);
 
function checkEmpty(){
if (
 ( !document.getElementById('user').value ) ||  
 (	!document.getElementById('sms').value) || 
 (	!document.getElementById('pw').value) || 
 (	!document.getElementById('repw').value) || 
 (	!document.getElementById('tel').value) || 
 (	!document.getElementById('email').value) || 
 (	!document.getElementById('name1').value) || 
 (	!document.getElementById('name2').value) ||  	
 ( 	!document.getElementById('name3').value) || 
 (	!document.getElementById('name4').value) 
 )
 	alert ("You must fill all information!");
 else {
   var answer = confirm("Are you sure you want to submit?");
   if (answer){
   	  window.location = "view.html";
	}
 }
}

//clear all field
var clear = document.getElementById('clear');
clear.addEventListener('click', clearfield,true);

function clearfield(){
    document.getElementById('user').value   = '';
    document.getElementById('sms').value    = '';
    document.getElementById('pw').value     = '';
    document.getElementById('repw').value   = '';
    document.getElementById('tel').value    = ''; 
    document.getElementById('email').value  = '';
    document.getElementById('name1').value  = '';
    document.getElementById('name2').value  = '';	
    document.getElementById('name3').value  = '';
    document.getElementById('name4').value  = '';
}


//

 
